//
//  ARTUserManager.m
//  YunDaHome-iOS
//
//  Created by 刘权 on 2019/11/1.
//  Copyright © 2019 YunDa. All rights reserved.
//

#import "ARTUserManager.h"

@interface ARTUserManager(){
    ARTUserInfoModel *_currentUser;
}

@end

@implementation ARTUserManager

+ (ARTUserManager *)defaultManager{
    static ARTUserManager *manager;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        manager = [[ARTUserManager alloc] init];
    });
    return manager;
}

/// 登录
- (void)loginByUser:(ARTUserInfoModel *)user{
    NSLog(@"登录成功");
    user.isLogin = YES;
    self.currentUser = user;
    if (self.loginSuccessBlock) {
        self.loginSuccessBlock();
    }
}

/// 退出
- (void)exist{
    // 删除当前用户信息
    ARTSingleShare.hadLoadTabVC = NO;
    J_UserDefault_RemoveObjectForKey(ART_Storage_DefaultDeviceUUID);
    if (ARTSingleShare.currentPeripheral && ARTSingleShare.baby) {
        [ARTSingleShare.baby cancelPeripheralConnection:ARTSingleShare.currentPeripheral];
        ARTSingleShare.baby = nil;
        ARTSingleShare.currentPeripheral = nil;
        ARTSingleShare.currentCharacteristic = nil;
        ARTSingleShare.writeCharacteristic = nil;
    }
    J_UserDefault_RemoveObjectForKey(ART_Storage_Token);
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:ART_Storage_CurrentUser];
    [[NSUserDefaults standardUserDefaults] synchronize];
    _currentUser = nil;
    if (self.existSuccessBlock) {
        self.existSuccessBlock();
    }
}

#pragma mark--GETTER && SETTER
- (ARTUserInfoModel *)currentUser{
    if (!_currentUser) {
        _currentUser = [ARTUserInfoModel new];//[ARTUserInfoModel getUser];
        _currentUser.userId = @"leslie";
    }
    return _currentUser;
}

- (void)setCurrentUser:(ARTUserInfoModel *)currentUser{
    if (currentUser) {
        _currentUser = currentUser;
        [currentUser updateUser];
    }
}

@end
